<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2023年5月12日
  Time: 8:54
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<link rel="stylesheet" href="webjars/bootstrap/3.4.1/dist/css/bootstrap.min.css">
<script src="webjars/jquery/3.6.0/dist/jquery.min.js"></script>
<script src="webjars/bootstrap/3.4.1/dist/js/bootstrap.min.js"></script>
<style>
    .container{
        margin-top: 10px;
    }
    .table{
        text-align: center;
    }
    .clearfix:after {
        content: '\20';
        display: block;
        height: 0;
        clear: both
    }
    .clearfix {
        zoom: 1
    }
</style>

<html>
<head>
    <title>学生列表展示</title>
</head>
<body>
    <div class="container">
        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">
                    <h4>
                        学生列表展示（SM整合）
                    </h4>
                </h3>
            </div>
            <table class="table table-hover table-striped">
                <tr>
                    <td>学号</td>
                    <td>姓名</td>
                    <td>性别</td>
                    <td>年龄</td>
                    <td>住址</td>
                    <td>所在班级</td>
                    <td>操作</td>
                </tr>
                <c:forEach items="${pr.rows}" var="stud">
                <tr>
                    <td>${stud.sid}</td>
                    <td>${stud.sname}</td>
                    <td>${stud.sex}</td>
                    <td>${stud.age}</td>
                    <td>${stud.addr}</td>
                    <td>${stud.cname}</td>
                    <td>
                        <div class="btn-group">
                            <a href="#" class="btn btn-primary btn-sm glyphicon glyphicon-pencil"
                               onclick="updateStudentUI(${stud.sid},'${stud.sname}','${stud.sex}',${stud.age},'${stud.addr}',${stud.cid})"></a>
                            <a href="/student?cmd=delete&sid=${stud.sid}" onclick="return confirm('真的要删除吗？')" class="btn btn-danger btn-sm glyphicon glyphicon-trash"></a>
                        </div>
                    </td>
                </tr>
                </c:forEach>
            </table>
            <div class="panel-footer text-right clearfix">
                <%--1. 条件查询--%>
                    <form class="form-inline" style="float:left;margin-top: 20px;" method="post" action="/student?cmd=search">
                        <input type="hidden" name="page" id="page" value="${param.page}">
                        <div class="form-group">
                            <input type="text" class="form-control" name="sname" value="${param.sname}" placeholder="输入学生姓名">
                        </div>
                        <div class="form-group">
                            <input type="text" class="form-control" name="addr" value="${param.addr}" placeholder="请输入学生住址">
                        </div>
                        <div class="form-group">
                            <select name="cid" class="form-control">
                                <option value="0">所有班级</option>
                                <c:forEach items="${classes}" var="c">
                                    <option value="${c.cid}" ${c.cid == param.cid ? 'selected' : ''}>${c.cname}</option>
                                </c:forEach>
                            </select>
                        </div>
                        <button type="submit" class="btn btn-default btn-sm glyphicon glyphicon-search">查询学生</button>
                        <button type="button" onclick="addStudentUI()" class="btn btn-info btn-sm glyphicon glyphicon-plus">添加学生</button>
                    </form>
                <%--2. 分页导航--%>
                    <nav aria-label="Page navigation">
                        <ul class="pagination">
                            <li>
                                <a href="#" aria-label="Previous">
                                    <span aria-hidden="true">&laquo;</span>
                                </a>
                            </li>
                            <c:forEach begin="1" end="${pr.totalPage}" var="p">
                             <li class="${p == param.page ? 'active' : ''}">
                                 <a href="javascript:;" onclick="skipPage(${p})">${p}</a>
                             </li>
                            </c:forEach>
                            <li>
                                <a href="#" aria-label="Next">
                                    <span aria-hidden="true">&raquo;</span>
                                </a>
                            </li>
                        </ul>
                    </nav>
            </div>
        </div>
    </div>

    <%--模态框--%>
    <!-- Modal -->
    <div class="modal fade" id="myModal">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="title">添加学生</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" method="post" action="/student?cmd=add">
                        <input type="hidden" name="sid" id="sid">
                        <div class="form-group">
                            <label  class="col-sm-2 control-label">学生姓名</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="sname" id="sname"  placeholder="学生姓名">
                            </div>
                        </div>
                        <div class="form-group">
                            <label  class="col-sm-2 control-label">学生性别</label>
                            <div class="col-sm-10">
                                <div class="radio">
                                    <label>
                                        <input type="radio" name="sex" id="sex1" value="男" checked>男
                                    </label>
                                    <label>
                                        <input type="radio" name="sex" id="sex2" value="女">女
                                    </label>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label  class="col-sm-2 control-label">学生年龄</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" name="age" id="age" placeholder="学生年龄">
                            </div>
                        </div>
                        <div class="form-group">
                            <label  class="col-sm-2 control-label">学生住址</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="addr" name="addr" placeholder="学生住址">
                            </div>
                        </div>
                        <div class="form-group">
                            <label  class="col-sm-2 control-label">所在班级</label>
                            <div class="col-sm-10">
                                <select name="cid" class="form-control" id="cid">
                                    <c:forEach items="${classes}" var="c">
                                        <option value="${c.cid}">${c.cname}</option>
                                    </c:forEach>
                                </select>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" onclick="save()">保存</button>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
<script>
    //1. 点击分页按钮
    function skipPage(p){
        //1.1 为表单隐藏域中的page赋值
        $("#page").val(p)
        //1.2 提交表单
        document.forms[0].submit()
    }
    //2. 显示添加对话框
    function addStudentUI() {
        $("#title").html("添加学生")
        document.forms[1].reset()
        $('#myModal').modal('show')
    }
    //3. 保存学生
    function save() {
        //3.1 如何区分是添加还是修改操作呢？看sid是否有值，如果有值就是修改
        let sid = $("#sid").val()
        if(sid){    // 修改操作
           document.forms[1].action = "/student?cmd=update"
        }
        // else{      // 添加操作
        //
        // }
        document.forms[1].submit()
    }
    //4. 显示修改学生界面
    function updateStudentUI(sid,sname,sex,age,addr,cid){
        //4.1 修改对话框的标题
        $("#title").html('修改学生')
        //4.2 为表单控件赋值
        $("#sid").val(sid)
        $("#sname").val(sname)
        $("#addr").val(addr)
        $("#age").val(age)
        $("#cid").val(cid)
        $("#sex1").prop("checked",sex === '男')
        $("#sex2").prop("checked",sex === '女')
        //4.3 打开对话框
        $("#myModal").modal("show")
    }
</script>
